const router = require('express').Router()
const config = require('../../../public/javascripts/config')
const db = require('../../../public/javascripts/db')
router.get('/', (req, res) => {
    var err
    var query = req.query
    var frameName = config.getConfigInfo().frame
    // console.log(frameName)
    var { measureType } = query
    if (measureType) {
        if (!['normal', 'vat', 'spc', 'crank', 'roller'].includes(measureType)) err = err ? err + 'measure参数错误' : 'measure参数错误'
    }
    else
        query.measureType = frameName
    if (frameName == 'normal' || frameName == 'vat') {
        var { page, perpage, order, order_by, fliter_by_judge, fliter_by_time } = query
        if (page && page == -1) {
            query.page = null
            query.perpage = null
        }
        if (!order_by) query.order_by = 'id'
        if (!order) query.order = 'DESC'

        if (page && page < 1) err = err ? err + 'page参数错误' : 'page参数错误'
        if (perpage && perpage <= 0) err = err ? err + 'perpage参数错误' : 'perpage参数错误'
        if (!err && query.page) query.page = (page - 1) * perpage
        if (order && order != 'DESC' && order != 'ASC') err = err ? err + 'order参数错误' : 'order参数错误'
        if (fliter_by_judge && !['OK', 'NG'].includes(fliter_by_judge)) err = err ? err + 'judge参数错误' : 'judge参数错误'
        if (fliter_by_time) {
            var stampArr = fliter_by_time.split('_')
            if (!stampArr || (stampArr && stampArr[0] > stampArr[1]))
                err = err ? err + 'time参数错误' : 'time参数错误'
        }

        if (err) res.status(405).json({ msg: err })
        else {

            db.getRecord(query, (data) => {
                res.status(200).json(data)
            })

        }
    }


})

router.delete('/', (req, res) => {
    // console.log(req.body)
    var err
    var params = req.body
    var frameName = config.getConfigInfo().frame
    // console.log(frameName)
    if (params.measureType) {
        if (!['normal', 'vat', 'spc', 'crank', 'roller'].includes(params.measureType)) err = err ? err + 'measure参数错误' : 'measure参数错误'
    }
    else
        params.measureType = frameName
    // if (params.ids && params.ids.length == 0) err = err ? err + 'ids参数错误' : 'ids参数错误'
    if (err) res.status(405).json({ msg: err })
    else {
        db.deleteRecord(params, () => {
            res.status(200).json({msg:'删除成功'})
        })
    }
})
module.exports = router